import requests, pandas as pd, time, random
from lxml import etree

# 定义AI相关的关键词列表
KEY_WORDS = ["人工智能", "AI", "算法", "机器学习", "深度学习"]

COMPANY_LIST = pd.read_excel("company_list.xlsx")["company_name"].tolist()  # 一列公司全称
# HTTP请求头
HEADERS = {"User-Agent": "Mozilla/5.0"}

def get_job_ratio(comp):
    url = f"https://sou.zhaopin.com/?kw={comp}&p=1"
    # HTTP请求
    r = requests.get(url, headers=HEADERS, timeout=10)
    # 解析HTML
    html = etree.HTML(r.text)
    # XPath提取
    total_jobs = html.xpath('//span[@class="search-result__num__1"]/text()')
    total = int(total_jobs[0]) if total_jobs else 0

    ai_jobs = 0
    # 遍历关键词
    for kw in KEY_WORDS:
        url_kw = f"https://sou.zhaopin.com/?kw={comp}+{kw}&p=1"
        r = requests.get(url_kw, headers=HEADERS, timeout=10)
        # 解析HTML
        html = etree.HTML(r.text)
        t = html.xpath('//span[@class="search-result__num__1"]/text()')
        ai_jobs += int(t[0]) if t else 0
        # 随机等待
        time.sleep(random.uniform(1, 2))

    ratio = ai_jobs / total if total else 0
    # 返回字典
    return {"company": comp, "total": total, "ai_jobs": ai_jobs, "ai_ratio": ratio}

# 结果列表
res = []
# 遍历列表
for c in COMPANY_LIST:
    try:
        # 添加到结果列表
        res.append(get_job_ratio(c))
    except Exception as e:
        # 异常处理
        print(c, e)
    time.sleep(random.uniform(2, 4))

# 保存为CSV
pd.DataFrame(res).to_csv("ai_job_ratio_zhaopin.csv", index=False, encoding="utf_8_sig")
